package core.comparators;
import core.User;
import java.util.Comparator;


/**
 *
 * @author Administrator
 */
public class UserComparator {

    public static enum USERCOMPARATORTYPE
    {
        ID,
        FIRSTNAME,
        LASTNAME,
        EMAIL,
        LEVEL
    };
    public Comparator getComparator(USERCOMPARATORTYPE type, boolean ascending)
    {
        switch(type)
        {

            case FIRSTNAME:
                return new CompareFname(ascending);
            case LASTNAME:
                return new CompareLname(ascending);
            case EMAIL:
                return new CompareEmail(ascending);
            case LEVEL:
                return new CompareLevel(ascending);
            default:
            case ID:
                return new CompareId(ascending);
            
        }
        
    }

    private class CompareFname implements Comparator
    {
        boolean ascending;
        CompareFname(boolean ascending)
        {
            this.ascending = ascending;
        }
        public int compare(Object o1, Object o2) {
                User b1 = (User) o1;
                User b2 = (User) o2;

                //if equal compare last name
                if(b1.getFname().compareTo(b2.getFname()) == 0)
                return ascending ?
                b1.getLname().compareTo(b2.getLname()) :
                b2.getLname().compareTo(b1.getLname());

            return ascending ?
                b1.getFname().compareTo(b2.getFname()) :
                b2.getFname().compareTo(b1.getFname());
        }
    }

        private class CompareLname implements Comparator
    {
        boolean ascending;
        CompareLname(boolean ascending)
        {
            this.ascending = ascending;
        }
        public int compare(Object o1, Object o2) {
                User b1 = (User) o1;
                User b2 = (User) o2;

                // if equal, compare first name
                if(b1.getLname().compareTo(b2.getLname()) == 0)
                return ascending ?
                b1.getFname().compareTo(b2.getFname()) :
                b2.getFname().compareTo(b1.getFname());


                return ascending ?
                b1.getLname().compareTo(b2.getLname()) :
                b2.getLname().compareTo(b1.getLname());
        }
    }
            private class CompareEmail implements Comparator
    {
        boolean ascending;
        CompareEmail(boolean ascending)
        {
            this.ascending = ascending;
        }
        public int compare(Object o1, Object o2) {
                User b1 = (User) o1;
                User b2 = (User) o2;

            return ascending ?
                b1.getEmail().compareTo(b2.getEmail()) :
                b2.getEmail().compareTo(b1.getEmail());
        }
    }
    
    private class CompareLevel implements Comparator
    {
        boolean ascending;
        CompareLevel(boolean ascending)
        {
            this.ascending = ascending;
        }
        public int compare(Object o1, Object o2) {
                User b1 = (User) o1;
                User b2 = (User) o2;

                Long v1 = new Long(b1.getLevel());
                Long v2 = new Long(b2.getLevel());

            return ascending ?
                v1.compareTo(v2) :
                v2.compareTo(v1);
        }
    }
    private class CompareId implements Comparator
    {
        boolean ascending;
        CompareId(boolean ascending)
        {
            this.ascending = ascending;
        }
        public int compare(Object o1, Object o2) {
                User b1 = (User) o1;
                User b2 = (User) o2;

                Long v1 = new Long(b1.getId());
                Long v2 = new Long(b2.getId());

            return ascending ?
                v1.compareTo(v2) :
                v2.compareTo(v1);
        }
    }

}
